﻿using EmbeddedSharp.Core;
using Zove.Runtime.Core.Expression;

namespace Zove.Scripting.Core.Functions {
    /// <summary>
    /// Purify the expression
    /// </summary>
    public class FormulateExpressionTranslator : DefaultExpressionTranslator {
        
        protected readonly string Subject;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="subject"></param>
        public FormulateExpressionTranslator(string subject) {
            Subject = subject;
        }

        /// <summary>
        /// Dealt with the terminal node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        protected override string ProcessTerminalNode(Node node) {
            if (node is FactorNode) {
                var fn = node as FactorNode;
                if (fn.Value != Subject) {
                    return fn.Factor.Value.ToString();
                }
                return fn.Value;
            }

            return base.ProcessTerminalNode(node);
        }
    }
}